
$(function(){
	
	//检察是否登录
	if(!checkLogin()){
		$(".loginTop").css("display", "none")
	}
		
	
	//接收从index.html传过来的查询条件的存储变量
	var key,city
	var selectType = '职位'
	var url = decodeURI(window.location.href.split("?")[1]);
	//alert(url)
	//判断是否为查询条件为职位信息
	if(url.indexOf('selectType=职位') != -1){
		
		//$("#search_input").val(url.split('=')[1])
		
		key = url.split('&')[0]
		key = key.split("=")[1]
		
		$("#search_input").val(key)
		console.log("数据1：", $("#search_input").val())
		//$("#search_input").val(key)
	}else if(url.indexOf('selectType=公司') != -1){//查询条件为公司名
		
		key = url.split('&')[0]
		key = key.split("=")[1]
		
		$("#search_input").val(key)
		console.log("数据2：", $("#search_input").val())
		selectType = '公司'
		if(selectType == "公司"){//查询条件为公司名就切换查询条件的下拉框
			
			$("#searchType li").each(function () {
				if($(this).text() == "公司"){
					$(this).siblings("li").hide(), $(this).hasClass("type_selected") || ($("#searchType li").removeClass(
						"type_selected"), $(this).addClass("type_selected").prependTo("#searchType"));
					var b = $(this).attr("data-searchtype");
					1 == b ? ($("#search_input").attr("placeholder", "请输入职位名称，如：产品经理"), $(".placeholder").val("请输入职位名称，如：产品经理")) : ($(
						"#search_input").attr("placeholder", "请输入公司名称，如：牛币"), $(".placeholder").val("请输入公司名称，如：牛币")), $("#spcInput").val(
						b), $(this).children("li").not(".type_selected").hide(), $(this).siblings(".searchtype_arrow").removeClass(
						"transform")
				}
			})	
		$("#selectType").val("公司")
		//设置搜索框中的内容
		
		
		}
		
	}
	
	
	
	
	
	//把浏览器的url内容换成当前面的html
	if(url != 'undefined'){
		var stateObject = {};
		var title = "Wow Title";
		var newUrl = "list.html";
		history.pushState(stateObject,title,newUrl)
	}
	$("#search_input").val(key)
	console.log("数据3：", $("#search_input").val())
	selectInfo(key, selectType, city)
	console.log("数据4：", $("#search_input").val())
	
	
	

function selectInfo(key1, selectType, city){
	console.log("数据5：", key1)
	$.ajax({
		url: path + "companyPosition/selectPositionAndCompany",
		type: "POST",
		data: JSON.stringify({"key":key1,selectType:selectType,city:city}),
		dataType: "json",
		crossDomain: true,
		xhrFields: {
		withCredentials: true
		},
		contentType: 'application/json',
		async: false,
		}).done(function(data) {
			if(data.requestStatus != undefined){
				modifyInfo(data)
			}
			setPager(data.page)
			$("#search_input").val(key1)
	})
	
}
	
//月薪范围的选择
var previousSalary
$("#salaryRange div").click(function(){
	//console.log($(this).text())
	if(previousSalary){
		previousSalary.removeClass("currrentSelected")
	}
	if(previousSalary && previousSalary.text() == $(this).text()){
		
		return
	}
	$(this).addClass("currrentSelected")
	previousSalary = $(this)
})



//工作经验的选择
var previousWork
$("#workExperience div").click(function(){
	//console.log($(this).text())
	if(previousWork){
		previousWork.removeClass("currrentSelected")
	}
	if(previousWork && previousWork.text() == $(this).text()){
		
		return
	}
	$(this).addClass("currrentSelected")
	previousWork = $(this)
})


//最低学历的选择
var previousEducation
$("#lowEducation div").click(function(){
	//console.log($(this).text())
	if(previousEducation){
		previousEducation.removeClass("currrentSelected")
	}
	if(previousEducation && previousEducation.text() == $(this).text()){
		
		return
	}
	$(this).addClass("currrentSelected")
	previousEducation = $(this)
})


//工作性质的选择
var previousNuture
$("#workNuture div").click(function(){
	//console.log($(this).text())
	if(previousNuture){
		previousNuture.removeClass("currrentSelected")
	}
	if(previousNuture && previousNuture.text() == $(this).text()){
		
		return
	}
	$(this).addClass("currrentSelected")
	previousNuture = $(this)
})


//发布时间的选择
var previousCreate
$("#creatTime div").click(function(){
	//console.log($(this).text())
	if(previousCreate){
		previousCreate.removeClass("currrentSelected")
	}
	if(previousCreate && previousCreate.text() == $(this).text()){
		
		return
	}
	$(this).addClass("currrentSelected")
	previousCreate = $(this)
})

//热门搜索
var previousHotFind
$("#hotFind dd a").click(function(){
	//console.log($(this).text())
	if(previousHotFind){
		previousHotFind.removeClass("currrentSelected")
	}
	if(previousHotFind && previousHotFind.text() == $(this).text()){
		$("#search_input").val("")
		return
	}
	$(this).addClass("currrentSelected")
	previousHotFind = $(this)
	
		$("#searchType").html("")
		$("#searchType").html(`
			<li data-searchtype="1" id="position" class="type_selected">职位</li>
			<li data-searchtype="4" id="company">公司</li>
		`)
		$("#search_input").attr("placeholder", "请输入职位名称，如：产品经理"), $(".placeholder").val("请输入职位名称，如：产品经理")
		$("#selectType").val('职位')
		//职位和公司的下拉框切换
		$("#searchType").hover(function() {
		 	$(this).children("li").show(), $(this).siblings(".searchtype_arrow").addClass("transform")
		 }, function() {
		 	$(this).children("li").not(".type_selected").hide(), $(this).siblings(".searchtype_arrow").removeClass(
		 		"transform")
		 }), $("#searchType li").click(function() {
		 	$(this).siblings("li").hide(), $(this).hasClass("type_selected") || ($("#searchType li").removeClass(
		 		"type_selected"), $(this).addClass("type_selected").prependTo("#searchType"));
		 	var b = $(this).attr("data-searchtype");
		 	1 == b ? ($("#search_input").attr("placeholder", "请输入职位名称，如：产品经理"), $(".placeholder").val("请输入职位名称，如：产品经理")) : ($(
		 		"#search_input").attr("placeholder", "请输入公司名称，如：牛币"), $(".placeholder").val("请输入公司名称，如：牛币")), $("#spcInput").val(
		 		b), $(this).children("li").not(".type_selected").hide(), $(this).siblings(".searchtype_arrow").removeClass("transform")
		
			
			if(b == 1){
				$("#selectType").val('职位')
			}else{
				$("#selectType").val('公司')
			}
			
			
			})
		
		
		
		
	
	
	
	//设置搜索框中的内容
	$("#search_input").val($(this).text())
	
	if(($(this).text()) == "实习"){
		$("#workNature").val($(this).text())
		$("#key").val("")
		console.log($(this).text())
	}else{
		$("#key").val($(this).text())
	}
	
	$("#search_button").click()
})

//工作地点
var previousPositionAddr
$("#workplaceSelect dd a").click(function(){
	//console.log($(this).text())
	
	
	
	if(previousOtherCity){
		previousOtherCity.css("background-color","#fafafa")
		$("#otherCity").removeClass("current")
	}
	
	if(previousPositionAddr){
		previousPositionAddr.removeClass("current")
	}
	if($(this).text != "全国"){
		$("#country").removeClass("current")
	}
	
	if($(this).hasClass("current")){
		$(this).removeClass("current")
		$("#cityInput").val("")
		
	}else{
		$(this).addClass("current")
		$("#cityInput").val($(this).text())
	}
	
	previousPositionAddr = $(this)
})

//其它里面的城市
var previousOtherCity
$("#box_expectCity dd span").click(function(){
	$("#cityInput").val($(this).text())
	// $(this).css("background-color","#fafafa")
	$("#country").removeClass("current")
	$("#otherCity").addClass("current")
	
	if(previousPositionAddr.text() !="其他"){
		previousPositionAddr.removeClass("current")
	}
	
	if(previousOtherCity){
		previousOtherCity.css("background-color","#fafafa")
	}
	$(this).css("background-color","#0D9572")
	previousOtherCity = $(this)
	
	$("#search_button").click()
	
})

//搜索按键单击
$("#search_button").click(function(){
	//console.log("单击")
	
	if($("#search_input").val() == ""){
		$("#search_input").val(key)
	}
	
	console.log("数据", $("#search_input").val())
	request = {}
	 var param = $('#searchForm').serializeArray();
	 $.each(param, function(i, v){
		 //console.log(i,v)
		 request[v['name']] = v['value']
	 })
	 
	 //console.log(request)
	 
	 $.ajax({
	 	url: path + "companyPosition/selectPositionAndCompany",
	 	type: "POST",
	 	data: JSON.stringify(request),
	 	dataType: "json",
	 	crossDomain: true,
	 	xhrFields: {
	 	withCredentials: true
	 	},
	 	contentType: 'application/json',
	 	async: false,
	 	}).done(function(data) {
	 	
		if(data.requestStatus != undefined){
			modifyInfo(data)
		}else{
			$("#selectInfo").html("")
			setPager(data.page)
		}
		
	 })
	 $("#pageNo").val("1")
})




function modifyInfo(data){
	//console.log(data)
	var company = data.company
	var position = data.position
	var founder = data.founder
	var mStr = ""
	var p
	var c
	var f
	setPager(data.page)
	
	for(i = 0; i<company.length; i++ ){
		p = position[i]
		c = company[i]
		f = founder[i]
		
		mStr += `<li class="odd clearfix">
			<div class="hot_pos_l">
				<div class="mb10">
					<a href="jobdetail.html?id=${p.id}" title="${p.positionType}" target="_blank">${p.positionType}</a>
					&nbsp;
					<span class="c9">[${p.positionAddress}]</span>
				</div>
				<span><em class="c7">月薪：</em>${p.salaryMin}k-${p.salaryMax}k</span>
				<span><em class="c7">经验：</em> ${p.workYear}</span>
				<span><em class="c7">最低学历： </em>${p.education}</span>
				<br />
				<span><em class="c7">职位诱惑：</em>${p.positionDetail}</span>
				<br />
				
				<span style="color:blue;">${p.createDate}</span>
			</div>
			<div class="hot_pos_r">
				<div class="apply">
					<a href="jobdetail.html?id=${p.id}" target="_blank">投个简历</a>
				</div>
				<div class="mb10"><a href="viewCompany.html?id=${c.userId}" title="${c.name}" target="_blank">${c.name}</a></div>
				<span><em class="c7">领域： </em>${c.domain}</span>
				<span><em class="c7">创始人：</em> ${f}</span>
				<br />
				<span><em class="c7">阶段： </em>${c.invest}</span>
				<span><em class="c7">规模： </em>${c.scale}</span>
				<ul class="companyTags reset" id = "tags">`
				tags = c.tag.split(",")
				//console.log(tags)
				for(j = 0; j <tags.length; j++){
					mStr += "<li>"+ tags[j] +"</li>"
				}
				mStr += "</ul></div></li>"
				
					
		
		
		
	}
	$("#selectInfo").html(mStr)
	
}






//职位和公司的下拉框切换
$("#searchType").hover(function() {
 	$(this).children("li").show(), $(this).siblings(".searchtype_arrow").addClass("transform")
 }, function() {
 	$(this).children("li").not(".type_selected").hide(), $(this).siblings(".searchtype_arrow").removeClass(
 		"transform")
 }), $("#searchType li").click(function() {
 	$(this).siblings("li").hide(), $(this).hasClass("type_selected") || ($("#searchType li").removeClass(
 		"type_selected"), $(this).addClass("type_selected").prependTo("#searchType"));
 	var b = $(this).attr("data-searchtype");
 	1 == b ? ($("#search_input").attr("placeholder", "请输入职位名称，如：产品经理"), $(".placeholder").val("请输入职位名称，如：产品经理")) : ($(
 		"#search_input").attr("placeholder", "请输入公司名称，如：牛币"), $(".placeholder").val("请输入公司名称，如：牛币")), $("#spcInput").val(
 		b), $(this).children("li").not(".type_selected").hide(), $(this).siblings(".searchtype_arrow").removeClass("transform")

	
	if(b == 1){
		$("#selectType").val('职位')
	}else{
		$("#selectType").val('公司')
	}
	
	
	})
})

var ttt = 1

//设置页码显示的样式
function setPager(pageInfo){
	var str=""
	var nextPager = pageInfo.nextPage
	var prePager = pageInfo.prePage
	var pages = pageInfo.pages
	var pageNumb = pageInfo.pageNum
	var start = pageNumb - 2
	var end = pageNumb + 2
	
	$('.Pagination').html("")
	
	
	if(prePager == 0){
		prePager = 1
	}
	if(pageInfo.nextPage == 0){
		nextPager = pages
	}
	str+='<div class="testtest"><a href="#" title="1" onclick="sendPager(1)">首页</a>'
	str+='<span class="disabled" title="上一页" onclick="sendPager('+prePager+')">上一页 </span>'
	if(start <=0){
		start = 1
		end += 2 
	}
	if(end > pages){
		
		start = start - (end - pages)
		if(start <= 0){
			start = 1
		}
		end = pages
	}
	if(end - start > 4){
		end = start + 4
	}
	
	
	for(start; start<=end ;start++){
		if(ttt == start || end == 1){
			str+='<span class="current"  style="background-color: #0061A6;" title="'+start+'" onclick="sendPager('+start+')">'+start+'</span>'
		}else{
			str+='<span class="current" title="'+start+'" onclick="sendPager('+start+')">'+start+'</span>'
		}
		
	}
	
	
	//'<span class="current" title="1">1</span><a href="#" title="2">2</a><a href="#" title="3">3</a>'
	str+='<a href="#" title="2" onclick="sendPager('+nextPager+',this)">下一页 </a><a href="#" onclick="sendPager('+pageInfo.lastPage+')">尾页</a></div>'
	
	$('.Pagination').html(str)
	
	
}

//页码被单击
function sendPager(page,t){
	ttt = page
	
	
	$("#pageNo").val(page)
	$("#search_button").click()
}




